<?

require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Smarty" . DIRECTORY_SEPARATOR . "Smarty.class.php");
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "config.class.php");
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "security.class.php");

function initialize_smarty() {
    global $smarty;
    if (!$smarty) {
        $smarty = new Smarty;
        smarty_config();
    }
}

function limpa_xml($string) {
    return urlencode($string);
}

function get_email_template($template) {
    global $smarty;
    initialize_smarty();
    $body = $smarty->fetch('emails' . DIRECTORY_SEPARATOR . $template . '.tpl');
    $lines = explode("\n", $body);
    $body = join("\n", $lines);
    return $body;
}

function exibe($exibir, $retornar = false) {
    if (!$retornar) {
        if (is_array($exibir) || is_object($exibir)) {
            echo "<pre>";
            print_r($exibir);
            echo "</pre>";
        } else {
            echo $exibir . "<br/><br/>";
        }
    } else {
        if (get('exibe')) {
            assign('exibir', $exibir);
        }
    }
}

function exibe_query($sql, $dados) {
    exibe(dao($sql, $dados));
    return dao($sql, $dados);
}

function smarty_config() {
    GLOBAL $smarty;
    $smarty->compile_check = get_config('compile_check');
    $smarty->cache_lifetime = get_config('tempo_cache');


    switch (get_config('nivel_cache')) {
        case 1:
            //cache somente do smarty
            $smarty->caching = true;
            break;
        case 3:
            //cache do smarty e do AdoDB
            $smarty->caching = true;
            break;
        case 5:
            //cache do smarty e do memcache
            $smarty->caching = true;
            break;
        case 7:
            //cache do smarty, memcache e AdoDB
            $smarty->caching = true;
            break;
        default:
            //sem cache do Smarty
            $smarty->caching = false;
            break;
    }

    if (!$_SERVER['SERVER_NAME']) {
        $_SERVER['SERVER_NAME'] == 'tmp';
    }
    $d = get_dominio();

    $cache = limpa_espacos(get_config('temp_dir') . DIRECTORY_SEPARATOR . $d['dominio'] . DIRECTORY_SEPARATOR . "smarty" . DIRECTORY_SEPARATOR . "cache" . DIRECTORY_SEPARATOR);
    $compile = limpa_espacos(get_config('temp_dir') . DIRECTORY_SEPARATOR . $d['dominio'] . DIRECTORY_SEPARATOR . "smarty" . DIRECTORY_SEPARATOR . "compile" . DIRECTORY_SEPARATOR);

    $template = limpa_espacos(get_template_dir());
    check_dir_exists($compile);
    check_dir_exists($cache);

    $smarty->template_dir = $template;
    $smarty->compile_dir = $compile;
    $smarty->left_delimiter = '<!--{';
    $smarty->right_delimiter = '}-->';
    //$smarty->config_dir = '';
    $smarty->cache_dir = $cache;
}

function check_dir_exists($dir) {
    if (!is_dir($dir)) {
        @mkdir($dir, 0777, true);
    }
}

function assign($nome, $dados) {
    GLOBAL $smarty;
    initialize_smarty();
    GLOBAL $all_vars;
    $all_vars[$nome] = $dados;

    if ($smarty) {
        if ($smarty->_tpl_vars[$nome] && is_array($smarty->_tpl_vars[$nome])) {
            if (is_array($dados)) {
                $dados = array_merge($dados, $smarty->_tpl_vars[$nome]);
            } else {
                $d = $smarty->_tpl_vars[$nome];
                $d[] = $dados;
                $dados = $d;
            }
        }
        @$smarty->assign($nome, $dados);
        if (get('exibe') == $nome) {
            exibe($dados);
        }
    }
}

function get_template_dir() {
    $template = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'comum' . DIRECTORY_SEPARATOR;
    return $template;
}

function display($tpl = null) {
    GLOBAL $smarty;
    initialize_smarty();
    GLOBAL $all_vars;
    if ($smarty) {
        @$smarty->assign('all_vars', $all_vars);
    }
    if (!$tpl) {
        $tpl = get_tpl_padrao();
    }
    $smarty->display(limpa_espacos(get_template_dir() . DIRECTORY_SEPARATOR . $tpl), get_cache_id($_SERVER['REQUEST_URI']));
}

function get_tpl_padrao() {
    $display = 'index.tpl';
    return $display;
}

function get_cache_id($id) {
    return md5($id);
}

?>
